function requiredUp(obj) {
    if (isNaN(Number(obj.value)) || Number(obj.value) < 0) {
        obj.value = "";
    }
    if (Number(obj.value) <= Number($("#moneyLowerLimit").val())) {
        layer.msg("需要大于第1个输入框的金额。", {
            icon: 5,
            time: 3000
        });
        $("#moneyUpperLimit").val("");
    }
}

function requiredMin(obj) {
    if (isNaN(Number(obj.value)) || Number(obj.value) < 0) {
        obj.value = "";
    }
    if (obj.value > Number($("#moneyUpperLimit").val())) {
        $("#moneyUpperLimit").val("");
    }
}

if (parent.levelId > 0) {
    $("#gradeId").val(parent.levelId);
    $.ajax({
        url: root.member.getLevelsById,
        cache: false, //禁用缓存
        dataType: "json",
        type: "post",
        contentType: 'application/x-www-form-urlencoded',
        data: {gradeId: parent.levelId},
        success: function (data) {
            if (data.errorCode == 0) {
                $("#gradeName").val(data.data.gradeName);
                $("#moneyLowerLimit").val(data.data.moneyLowerLimit);
                $("#moneyUpperLimit").val(data.data.moneyUpperLimit);
                $("#gradeExplain").val(data.data.gradeExplain);
                $("#gradeCoefficient").val(data.data.gradeCoefficient);
                $("#state").val(data.data.state);
            } else {
                layer.msg(data.sub_msg, {
                    icon: 2,
                    time: 3000
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            console.log(textStatus);
            console.log(errorThrown);
            console.log("!!!!会员等级系统异常!!!!");
            layer.msg('会员等级系统异常!', {
                icon: 2,
                time: 3000
            });
            layer.closeAll();
        }
    });
}

// 表单提交
$(function () {
    $("#form-level").validate({
        submitHandler: function (from) {
            var url = ""
            if (parent.levelId > 0) {
                url = root.member.updateLevel
            } else {
                url = root.member.saveLevel;
            }
            $(from).ajaxSubmit({
                dataType: "json",
                type: "post",
                contentType: 'application/x-www-form-urlencoded',
                url: url,
                success: function (data) {
                    if (data.errorCode == 0) {
                        layer.msg("保存成功", {
                            icon: 6,
                            time: 3000
                        });
                        setTimeout(function () {
                            parent.initTable();
                            removeThisIframe();
                        }, 3000);
                    } else {
                        layer.msg(data.sub_msg, {
                            icon: 2,
                            time: 3000
                        });
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    console.log(XmlHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                    layer.msg('会员等级系统异常!', {
                        icon: 5,
                        time: 3000
                    });
                }
            });
        }
    });
});



$.validator.addMethod("requiredCategory", function (value, element) {
    if ($("#oneCategoryId").val() == "" && $("#gradeId").val() == "") {
        return false;
    }
    return true;
}, "请至少选择一项，定义积分系数！");

/*
// 折扣限制输入
function reduce(obj) {
    obj.value = obj.value.replace(/\b(0+)/gi, ""); // 替换开头的0
    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字
    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
    if (obj.value != "" && obj.value < 1) {
        obj.value = 1;
    }
}
*/

/*关闭详情页*/
function removeThisIframe() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
function reduce22() {
    if($("#gradeCoefficient").val() >3){
        $("#gradeCoefficient").val("")
    }
}